package handler

import (
	"car-trade-sys/model"
	"github.com/gin-gonic/gin"
	"strconv"
)

func AddRoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	data := model.RN{
		Rid: rid,
		Nid: nid,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func UserRole(c *gin.Context) {
	uid := c.Query("uid")
	/*
		SELECT
		*
		FROM
		roles LEFT JOIN urs ON urs.uid=roles.id
		WHERE urs.uid=1
	*/
	var role []model.Role
	model.DB.Table("roles").
		Joins("LEFT JOIN urs ON urs.uid=roles.id").Where("urs.uid=?", uid).
		Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "OK",
		"data": role,
	})
	return
}

func RoleNode(c *gin.Context) {
	rid := c.Query("rid")
	/*
		SELECT
		*
		FROM
		nodes LEFT JOIN rns ON rns.nid=nodes.id
		WHERE rns.rid=1
	*/
	var node []model.Node
	model.DB.Table("nodes").
		Joins("LEFT JOIN rns ON rns.nid=nodes.id").Where("rns.rid=?", rid).
		Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "OK",
		"data": node,
	})
	return
}
